home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / xlib / tri2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.9 KB  |  251 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <GL/glx.h>
  18. #include <GL/glu.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <X11/keysym.h>
  22.  
  23. static int attributes[] = {
  24.     GLX_RGBA,
  25.     GLX_RED_SIZE, 1,
  26.     GLX_GREEN_SIZE, 1,
  27.     GLX_BLUE_SIZE, 1,
  28.     None,
  29. };
  30.  
  31. int width = 200, height = 200;
  32. GLUquadricObj *quadObj;            
  33.  
  34. static void DoDisplay(GLfloat tx, GLfloat ty, GLfloat rx, GLfloat rz)
  35. {
  36.     glMatrixMode(GL_PROJECTION);
  37.     glLoadIdentity();
  38.     glOrtho(-0.5, width - 0.5, -0.5, height - 0.5, 0.01, 1000.0);
  39.     glMatrixMode(GL_MODELVIEW);
  40.     glLoadIdentity();
  41.     glTranslatef(100 + tx, 100 + ty, -10);
  42.     glRotatef(rx, 1, 0, 0);
  43.     glRotatef(rz, 0, 0, 1);
  44.     glScalef(20, 20, 20);
  45.  
  46.     glViewport(0, 0, width, height);
  47.     glClearColor(0.25, 0.25, 0.25, 1.0);
  48.     glClear(GL_COLOR_BUFFER_BIT);
  49.     glShadeModel(GL_FLAT);
  50.  
  51.     /* Draw a bunch of triangles that converge on a single point */
  52.     glBegin(GL_TRIANGLE_FAN);
  53.     glColor3f(1, 0, 0);
  54.     glVertex2f(0, 0);
  55.     glVertex2f(-1, -0.5);
  56.     glVertex2f(-1, -0.25);
  57.     glColor3f(0, 1, 0);
  58.     glVertex2f(-1, 0);
  59.     glColor3f(0, 0, 1);
  60.     glVertex2f(-1, 0.25);
  61.     glColor3f(1, 1, 1);
  62.     glVertex2f(-1, 0.5);
  63.     glEnd();
  64.  
  65.     glBegin(GL_TRIANGLE_FAN);
  66.     glColor3f(1, 0, 0);
  67.     glVertex2f(0, 0);
  68.     glVertex2f(-0.5, 1);
  69.     glVertex2f(-0.25, 1);
  70.     glColor3f(0, 1, 0);
  71.     glVertex2f(0, 1);
  72.     glColor3f(0, 0, 1);
  73.     glVertex2f(0.25, 1);
  74.     glColor3f(1, 1, 1);
  75.     glVertex2f(0.5, 1);
  76.     glEnd();
  77.  
  78.     glBegin(GL_TRIANGLE_FAN);
  79.     glColor3f(1, 0, 0);
  80.     glVertex2f(0, 0);
  81.     glVertex2f(1, 0.5);
  82.     glVertex2f(1, 0.25);
  83.     glColor3f(0, 1, 0);
  84.     glVertex2f(1, 0);
  85.     glColor3f(0, 0, 1);
  86.     glVertex2f(1, -0.25);
  87.     glColor3f(1, 1, 1);
  88.     glVertex2f(1, -0.5);
  89.     glEnd();
  90.  
  91.     glBegin(GL_TRIANGLE_FAN);
  92.     glColor3f(1, 0, 0);
  93.     glVertex2f(0, 0);
  94.     glVertex2f(-0.5, -1);
  95.     glVertex2f(-0.25, -1);
  96.     glColor3f(0, 1, 0);
  97.     glVertex2f(0, -1);
  98.     glColor3f(0, 0, 1);
  99.     glVertex2f(0.25, -1);
  100.     glColor3f(1, 1, 1);
  101.     glVertex2f(0.5, -1);
  102.     glEnd();
  103.  
  104.     glPushMatrix();
  105.     gluQuadricDrawStyle (quadObj, GLU_FILL);
  106.     glColor3f (3.2, 0.8, 1.0);
  107.     gluDisk(quadObj,2.0,5.0,16,4);
  108.  
  109. }
  110.  
  111. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  112. {
  113.     if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
  114.     return GL_TRUE;
  115.     }
  116.     return GL_FALSE;
  117. }
  118.  
  119. int main()
  120. {
  121.     XVisualInfo *vi;
  122.     Display *dpy;
  123.     Colormap cmap;
  124.     Window window;
  125.     XSetWindowAttributes swa;
  126.     GLXContext cx;
  127.     XEvent event;
  128.     GLboolean needDisplay;
  129.     GLfloat tx = 0;
  130.     GLfloat ty = 0;
  131.     GLfloat rx = 0;
  132.     GLfloat rz = 0;
  133.  
  134.     dpy = XOpenDisplay(0);
  135.     if (!dpy) {
  136.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  137.     return -1;
  138.     }
  139.  
  140.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributes);
  141.     if (!vi) {
  142.     fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
  143.         getenv("DISPLAY"));
  144.     return -1;
  145.     }
  146.  
  147.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  148.                AllocNone);
  149.     swa.border_pixel = 0;
  150.     swa.colormap = cmap;
  151.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  152.     | KeyReleaseMask;
  153.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  154.                width, height,
  155.                0, vi->depth, InputOutput, vi->visual,
  156.                CWBorderPixel|CWColormap|CWEventMask, &swa);
  157.     XSetWMColormapWindows(dpy, window, &window, 1);
  158.     XMapWindow(dpy, window);
  159.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  160.  
  161.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  162.     if (!glXMakeCurrent(dpy, window, cx)) {
  163.     fprintf(stderr, "Can't make window current to context\n");
  164.     return -1;
  165.     }
  166.  
  167.     quadObj = gluNewQuadric ();
  168.  
  169.     needDisplay = GL_TRUE;
  170.     for (;;) {
  171.     do {
  172.         XNextEvent(dpy, &event);
  173.         switch (event.type) {
  174.           case Expose:
  175.         needDisplay = GL_TRUE;
  176.         break;
  177.           case ConfigureNotify:
  178.         width = event.xconfigure.width;
  179.         height = event.xconfigure.height;
  180.         needDisplay = GL_TRUE;
  181.         break;
  182.           case KeyPress:
  183.         {
  184.             char buf[100];
  185.             int rv;
  186.             KeySym ks;
  187.  
  188.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  189.             switch (ks) {
  190.               case XK_p:
  191.               case XK_P:
  192.             glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
  193.             needDisplay = GL_TRUE;
  194.             break;
  195.               case XK_l:
  196.               case XK_L:
  197.             glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  198.             needDisplay = GL_TRUE;
  199.             break;
  200.               case XK_f:
  201.               case XK_F:
  202.             glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  203.             needDisplay = GL_TRUE;
  204.             break;
  205.               case XK_Left:
  206.             tx -= 0.1;
  207.             needDisplay = GL_TRUE;
  208.             break;
  209.               case XK_Right:
  210.             tx += 0.1;
  211.             needDisplay = GL_TRUE;
  212.             break;
  213.               case XK_Up:
  214.             ty += 0.1;
  215.             needDisplay = GL_TRUE;
  216.             break;
  217.               case XK_Down:
  218.             ty -= 0.1;
  219.             needDisplay = GL_TRUE;
  220.             break;
  221.               case XK_KP_Left:
  222.             rz += 1;
  223.             needDisplay = GL_TRUE;
  224.             break;
  225.               case XK_KP_Right:
  226.             rz -= 1;
  227.             needDisplay = GL_TRUE;
  228.             break;
  229.               case XK_KP_Up:
  230.             rx += 1;
  231.             needDisplay = GL_TRUE;
  232.             break;
  233.               case XK_KP_Down:
  234.             rx -= 1;
  235.             needDisplay = GL_TRUE;
  236.             break;
  237.               case XK_Escape:
  238.             return 0;
  239.             }
  240.         }
  241.         break;
  242.         }
  243.     } while (XPending(dpy) != 0);
  244.  
  245.     if (needDisplay) {
  246.         needDisplay = GL_FALSE;
  247.         DoDisplay(tx, ty, rx, rz);
  248.     }
  249.     }
  250. }
  251.